package com.zxt.content.copywriting.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxt.content.copywriting.mapper.CopywritingTypeRelationMapper;
import com.zxt.content.copywriting.model.CopywritingTypeRelation;
import com.zxt.content.copywriting.req.CopywritingTypeRelationDTO;
import com.zxt.content.copywriting.req.PrivateCopywritingDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jinhong.geng
 * @date 2022年07月21日 14:20
 */
@Slf4j
@Service
public class CopywritingTypeRelationManager extends ServiceImpl<CopywritingTypeRelationMapper, CopywritingTypeRelation> {

    public List<CopywritingTypeRelation> typeRelationsList(List<Long> typeIds) {
        LambdaQueryWrapper<CopywritingTypeRelation> queryWrapper = CopywritingTypeRelation.gw();
        queryWrapper.in(CopywritingTypeRelation::getTypeId, typeIds);
        return this.list(queryWrapper);
    }

    public void delete(CopywritingTypeRelationDTO dto) {
        LambdaQueryWrapper<CopywritingTypeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopywritingTypeRelation::getCopywritingId, dto.getCopywritingId());
        queryWrapper.and(wrapper -> {
            wrapper.in(CopywritingTypeRelation::getTypeId, dto.getTypeIds());
            wrapper.eq(CopywritingTypeRelation::getIsPublic, dto.getIsPublic());
        });
        this.remove(queryWrapper);
    }

    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<CopywritingTypeRelation> queryWrapper = CopywritingTypeRelation.gw();
        queryWrapper.in(CopywritingTypeRelation::getCopywritingId, ids);
        this.remove(queryWrapper);
    }


    public List<CopywritingTypeRelation> getByPrivateCopywritingIds(List<Long> copywritingIds) {
        LambdaQueryWrapper<CopywritingTypeRelation> queryWrapper = CopywritingTypeRelation.gw();
        queryWrapper.in(CopywritingTypeRelation::getCopywritingId, copywritingIds);
        queryWrapper.eq(CopywritingTypeRelation::getIsPublic, Boolean.FALSE);
        return this.list(queryWrapper);
    }


    public void removeByPublicCopywritingIds(List<Long> existPublicCopywritingIds) {
        LambdaQueryWrapper<CopywritingTypeRelation> queryWrapper = CopywritingTypeRelation.gw();
        queryWrapper.in(CopywritingTypeRelation::getCopywritingId, existPublicCopywritingIds);
        queryWrapper.eq(CopywritingTypeRelation::getIsPublic, Boolean.TRUE);
        remove(queryWrapper);
    }
}
